﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace game
{
    public class ShipEnginePSystem : BillboardParticleSystem
    {
        public ShipEnginePSystem(Scene scene, Vector3 position)
            : base(scene, position)
        {
        }

        protected override void InitParameters()
        {
            textureName = "Textures\\8195-v6";
            effectName = "Effects\\BillboardParticle";
            effectTechName = "DynamicBillboardParticleAdditiveBlending";

            Acceleration = -0.00f;
            maxParticles = 150; //25

            originalEmitTimeMs = 20.0f;//30

            emitter = new ParticleEmitter(this, originalEmitTimeMs, position);

            MinInitialSpeed = 0.0f;
            MaxInitialSpeed = 0.0f;
            MinInitialSize = 90; //300
            MaxInitialSize = 130; //350
            MinLifeTimeMs = 150;
            MaxLifeTimeMs = 250;
        }

        protected float currentStrength = 4.0f;
        protected float maxStrength = 12.0f;
        protected float strengthROC = 0.02f; // ROC = rate of change

        protected float originalEmitTimeMs;
        protected float emitTimeMsModFactor = 1.5f;

        public void Update(GameTime gameTime, Vector3 position, Matrix rotation,
            Vector3 velocity, bool speedingUp)
        {
            if (speedingUp && currentStrength < maxStrength)
            {
                currentStrength += strengthROC * gameTime.ElapsedGameTime.Milliseconds;
                if (currentStrength > maxStrength)
                    currentStrength = maxStrength;
            }
            else if (!speedingUp && currentStrength > 4.0f)
            {
                currentStrength -= strengthROC * gameTime.ElapsedGameTime.Milliseconds;
                if (currentStrength < 4.0f)
                    currentStrength = 4.0f;
            }

            emitter.EmitTimeMs = originalEmitTimeMs - currentStrength * emitTimeMsModFactor;

            base.Update(gameTime, position, rotation, velocity);
        }

        protected override void InitParticle(int index, Vector3 position, float initialTimeMsOffset)
        {

            base.InitParticle(index, Vector3.Zero, initialTimeMsOffset);

            vertices[index].InitialPosition = position;
            vertices[index+1].InitialPosition = position;
            vertices[index+2].InitialPosition = position;
            vertices[index+3].InitialPosition = position;

            float speed = RandomHelper.RandomBetween(MinInitialSpeed, MaxInitialSpeed);
            Vector3 vel = Vector3.Transform(Vector3.Backward, rotation) * currentStrength + this.velocity;
            vertices[index].Velocity = vel;
            vertices[index + 1].Velocity = vel;
            vertices[index + 2].Velocity = vel;
            vertices[index + 3].Velocity = vel;
        }
    }
}
